package com.atom.common.web.controller;

import javax.annotation.Resource;

import org.activiti.engine.impl.util.json.JSONArray;
import org.activiti.engine.impl.util.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atom.common.remote.user.RemoteUserDao;

@Controller
@RequestMapping("remote/user")
public class RemoteUserController {

  private static Logger logger = Logger.getLogger(RemoteUserController.class);

  @Resource RemoteUserDao remoteUserDao;

  @RequestMapping(method=RequestMethod.GET, value="getOrgTree.do")
  @ResponseBody
  public String getOrgTree() {
    return remoteUserDao.getOrgTree();
  }

  @RequestMapping(method=RequestMethod.GET, value="getAllOrgs.do")
  @ResponseBody
  public String getAllOrgs() {
    return remoteUserDao.listOrgByOrgid("shandongbowuguan");
  }

  @RequestMapping(method=RequestMethod.GET, value="getOrgUser.do")
  @ResponseBody
  public String getOrgUser(@RequestParam(value = "orgid", required = true) String orgid) {
    return remoteUserDao.listUserByOrgid(orgid);
  }

  @RequestMapping(method=RequestMethod.GET, value="getFggz.do")
  @ResponseBody
  public String getFenGuanGuanZhang(@RequestParam(value = "orgid", required = false) String orgid) {
    JSONObject json = new JSONObject();
    try {
      JSONArray zgz = new JSONArray(remoteUserDao.queryUserInfo("positionid", "1"));
      JSONArray fgz = new JSONArray(remoteUserDao.queryUserInfo("positionid", "4"));
      JSONArray fggz = new JSONArray();
      for (int i=0; i<zgz.length(); i++) {
        fggz.put(zgz.get(i));
      }
      for (int i=0; i<fgz.length(); i++) {
        fggz.put(fgz.get(i));
      }
      json.put("data", fggz);
    } catch (Exception e) {
      logger.error("error message = {}" + e.getMessage(), e);
    }
    return json.toString();
  }

  @RequestMapping(method=RequestMethod.GET, value="verifyUseridAndPwd.do")
  @ResponseBody
  public String verifyUseridAndPwd(@RequestParam(value = "userid", required = true) String userid,
      @RequestParam(value = "password", required = true) String password) {
    return remoteUserDao.verifyUseridAndPwd(userid, password);
  }

  @RequestMapping(method=RequestMethod.GET, value="getUserByRole.do")
  @ResponseBody
  public String getUserByRole(@RequestParam(value = "roleId", required = true) String roleId) {
    return remoteUserDao.getUserByRole(roleId);
  }

  @RequestMapping(method=RequestMethod.GET, value="getZgzr.do")
  @ResponseBody
  public String getZgzr(@RequestParam(value = "orgid", required = true) String orgid) {
    JSONObject json = new JSONObject();
    JSONArray zgzr = new JSONArray();
    try {
      zgzr = new JSONArray(remoteUserDao.oaQuery(orgid, "主任,副主任"));
    } catch (Exception e) {
      logger.error("error message = {}" + e.getMessage(), e);
    }
    json.put("zgzr", zgzr);
    return json.toString();
  }
}
